** this file replicates the 4 graphs in Figure 2 in the main text.

clear all

**Set your path
*global path = ""

**Call the main data file
		use "$path/.../kswxy_data", clear

		
//SET CONTROLS:
global controls "contract_cons_exw97 chinatariff00_expw97 inputtariff02_expw97 share_revenue_exw97" 
label var contract_cons_exw97 "Contract Intensity"
label var share_revenue_exw97 "Export License"
label var chinatariff00_expw97 "Import Tariffs"
label var inputtariff02_expw97 "Input Tariffs"
label var ln_chinatariff_expw97 "Annual Import Tariffs"
label var ln_inputtariff_expw97 "Annual Input Tariffs"

**DROP CITY MISSING OCDE
drop if citycode==.
							   
			egen iv_bin = cut(iv_ntr), group(40)
		egen mean_iv_ntr=mean(iv_ntr), by(iv_bin)
		egen total_pop=total(pop), by(iv_bin)
		
		bys citycode: gen pct_dvalue_0013 = (dvalue - dvalue[_n-13])/dvalue[_n-13] if year==2013
		
			**** Shenzhen and Bejing are outlier city
		gen no_outlier_tot_0413=shc_stud_tot_0413 if (citycode!=4403 & citycode!=1100) 
		gen no_outlier_tot_0213=shc_stud_tot_0213 if (citycode!=4403 & citycode!=1100) 
		gen no_outlier_tot_0013=shc_stud_tot_0013 if (citycode!=4403 & citycode!=1100) 
		gen no_outlier_tot_0001 = shc_stud_tot_0001_pop04 if  (citycode!=4403 & citycode!=1100) 
		gen lnc_numstd_midschool2_9700 = lnc_numstd_midschool_9700 if  (citycode!=4403 & citycode!=1100) 
		gen lnc_num_midschool2_9700 = lnc_num_midschool_9700 if  (citycode!=4403 & citycode!=1100) 
					  
				
		label var shc_stud_tot_0213 "Growth in Students, 2002-2013"
		label var shc_stud_tot_0013 "Growth in Students, 2000-2013"
		label var no_outlier_tot_0213 "Growth in Students, 2002-13 (no outliers)"	
		label var no_outlier_tot_0013 "Growth in Students, 2000-13 (no outliers)"	
		label var no_outlier_tot_0001 "Growth in Students, 2000-1  (no outliers)"	
		label var shc_stud_tot_0001_pop04 "Students 2000-01 per 1000"
	label var lnc_gdp_9700 "Log Change in GDP, 1997-2000"
	label var lnc_emp_9700 "Log Change in Employment, 1997-2000"
	label var lnc_inv_fdi_9700 "Log Change in FDI, 1997-2000"
	label var lnc_inv_realest_9700 "Log Change in RE Investment, 1997-2000"
	label var lnc_dvalue_9700 "Exports, 1997-2000"
	label var lnc_numstd_college_9700 "College Students, 1997-2000"
	label var lnc_num_college_9700 "Number of Colleges, 1997-2000"
	label var lnc_numstd_midschool2_9700 "Middle School Students, 1997-2000"
	label var lnc_num_midschool2_9700 "Number of Middle Schools, 1997-2000"
	label var lnc_dvalue_0013 "Exports, 2000-2013"

	
				
	

	**** Here are the figures in panel Figure 2a *********
	
	
	foreach var in lnc_dvalue_0013  shc_stud_tot_0013 no_outlier_tot_0001 {
		egen mean_`var'=mean(`var'), by(iv_bin)	
	}

	foreach var in lnc_dvalue_0013  shc_stud_tot_0013 {
		twoway (scatter mean_`var' mean_iv_ntr [weight=total_pop], msymbol(circle_hollow)) ///
		(lfit `var' iv_ntr ) if year==2013, legend(off) ///
		xtitle("PNTR{sub:c} (Weighted NTR Gaps)", size(huge)) scheme(s1mono) name(A_`var') title("`: variable label `var''", size(huge)) legend(off) 
			
	}
	
	
		**** FIGURE 2b *********

	***3** EVENT STUDY.
		sort citycode year
			
			**CONSTRUCT YEAR ON YEAR CHANGES, STANDARDIZED BY 2000 POP
			foreach lvl in as ba ma dr tot {
		
				bys citycode: gen shc_stud_`lvl'_0102 = (stud_`lvl'[_n-11] - stud_`lvl'[_n-12]) / (pop[_n-13]) if year==2013
				bys citycode: gen shc_stud_`lvl'_0203 = (stud_`lvl'[_n-10] - stud_`lvl'[_n-11]) / (pop[_n-13]) if year==2013
				bys citycode: gen shc_stud_`lvl'_0304 = (stud_`lvl'[_n-9] - stud_`lvl'[_n-10]) / (pop[_n-13]) if year==2013
				bys citycode: gen shc_stud_`lvl'_0405 = (stud_`lvl'[_n-8] - stud_`lvl'[_n-9]) / (pop[_n-13]) if year==2013
				bys citycode: gen shc_stud_`lvl'_0506 = (stud_`lvl'[_n-7] - stud_`lvl'[_n-8]) / (pop[_n-13]) if year==2013
				bys citycode: gen shc_stud_`lvl'_0607 = (stud_`lvl'[_n-6] - stud_`lvl'[_n-7]) / (pop[_n-13]) if year==2013
				bys citycode: gen shc_stud_`lvl'_0708 = (stud_`lvl'[_n-5] - stud_`lvl'[_n-6]) / (pop[_n-13]) if year==2013
				bys citycode: gen shc_stud_`lvl'_0809 = (stud_`lvl'[_n-4] - stud_`lvl'[_n-5]) / (pop[_n-13]) if year==2013
				bys citycode: gen shc_stud_`lvl'_0910 = (stud_`lvl'[_n-3] - stud_`lvl'[_n-4]) / (pop[_n-13]) if year==2013
				bys citycode: gen shc_stud_`lvl'_1011 = (stud_`lvl'[_n-2] - stud_`lvl'[_n-3]) / (pop[_n-13]) if year==2013
				bys citycode: gen shc_stud_`lvl'_1112 = (stud_`lvl'[_n-1] - stud_`lvl'[_n-2]) / (pop[_n-13]) if year==2013
				bys citycode: gen shc_stud_`lvl'_1213 = (stud_`lvl' - stud_`lvl'[_n-1]) / (pop[_n-13]) if year==2013
				
			}
					
				**EVENT STUDY ANALYSIS
				gen b = .
				gen ciu95 = .
				gen cil95 = .
				local i =1
				foreach t in 0001 0102 0203 0304 0405 0506 0607 0708 0809 0910 1011 1112 1213 {
				eststo N_`t': reg shc_stud_tot_`t' iv_ntr if year==2013 & balanced==1, cluster(citycode)
				
				local y = 1999 + `i'
				replace b = _b[iv_ntr] if year == `y'
				replace ciu95 = _b[iv_ntr] + 1.96*_se[iv_ntr] if year == `y'
				replace cil95 = _b[iv_ntr] - 1.96*_se[iv_ntr] if year == `y'
				local i = `i'+1
				
				}
				
				preserve
				collapse (firstnm) b ci*, by(year)
				replace year=year+1
				
					forvalues y=1/13{
						local j=`y'-1+2000
						local k=`y'+2000
						label def Dyear `k' "`j'-`y'", add
					}
					
				label value year Dyear	
				
				replace b=0 if year==2000
				gr tw (scatter b year) (rcap ciu95 cil95 year, lpatter(dash)) if year>=2001, ytitle("Year-on-Year Change in Students per 1000") ///
				ylabel(-.1(0.05).25) xline(2002, lcolor(red)) xlabel(2001(1)2013, labels angle(forty_five) valuelabel) legend(off) scheme(s1mono) yline(0) title("Yearly Change in Students Due to PNTR{sub:c}")
			
				restore
				
				foreach var in   no_outlier_tot_0213 { 
						gen `var'_yoy = `var'/11
						egen mean_`var'_yoy=mean(`var'_yoy), by(iv_bin)
					}	
					
				twoway (scatter mean_no_outlier_tot_0213_yoy mean_iv_ntr [weight=total_pop], msymbol(circle_hollow)) ///
			(lfit no_outlier_tot_0213_yoy iv_ntr ) ///
			(scatter mean_no_outlier_tot_0001 mean_iv_ntr [weight=total_pop], mcolor(dimgray) msymbol(triangle_hollow)) ///
			(lfit no_outlier_tot_0001 iv_ntr ) if year==2013, legend(off) text(0.022 0.5 "Post Liberalization") text(0.004 0.5 "Pre Liberalization") ///
			xtitle("PNTR{sub:c} (Weighted NTR Gaps)", size(vlarge))  ytitle("Annual Year-on-Year Change", size(vlarge)) scheme(s1mono) name(A_yoy) title("Growth in Students (no outliers)", size(vlarge)) 
			

				 
